package com.hou.netty.serial;

import com.hou.netty.TestSystem.GzipUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.io.File;
import java.io.FileOutputStream;

//服务端业务处理(处理订单): 有序列化编解码.可直接接受吧发送对象
public class ServerHandler extends SimpleChannelInboundHandler<Order> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Order order) throws Exception {
        System.out.println("接受");
        System.out.println("接收到了订单:" + order.getName() + ",价格:" + order.getPrice());
        //返回数据给客户端
        //将byte数组解压缩并转成图片保存
        byte[] ungzip = GzipUtils.ungzip(order.getPicture());
        //保存路径
        String path = System.getProperty("user.dir") + File.separatorChar + "file" + File.separatorChar + "order.jpg";
        FileOutputStream fos = new FileOutputStream(path);//文件输出流
        fos.write(ungzip);//写到目标文件
        fos.close();
        Result result = new Result();
        result.setStatus("处理中");
        result.setMemo("您的订单正在正常处理");
        channelHandlerContext.writeAndFlush(result);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
